package com.ineatconseil.yougo.wicket.ui;

import com.ineatconseil.yougo.wicket.ui.auth.LogoutPage;
import com.ineatconseil.yougo.wicket.YougoSession;
import com.ineatconseil.yougo.wicket.ui.admin.panel.GererTypesDemandes;
import com.ineatconseil.yougo.wicket.ui.admin.panel.GererTypesUtilisateurs;
import com.ineatconseil.yougo.wicket.ui.admin.panel.GererUtilisateurs;
import com.ineatconseil.yougo.wicket.ui.request.panel.MesDemandes;
import com.ineatconseil.yougo.wicket.ui.request.panel.NouvelleDemande;
import com.ineatconseil.yougo.wicket.ui.request.panel.ValiderDemandes;
import java.util.ArrayList;
import java.util.List;
import org.apache.wicket.PageParameters;
import org.apache.wicket.authorization.strategies.role.Roles;
import org.apache.wicket.authorization.strategies.role.annotations.AuthorizeInstantiation;
import org.apache.wicket.extensions.markup.html.tabs.AbstractTab;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.extensions.markup.html.tabs.TabbedPanel;
import org.apache.wicket.markup.html.CSSPackageResource;
import org.apache.wicket.markup.html.link.BookmarkablePageLink;
import org.apache.wicket.markup.html.panel.FeedbackPanel;
import org.apache.wicket.model.Model;

/**
 * Homepage
 */
@AuthorizeInstantiation({Roles.USER, Roles.ADMIN})
public class HomePage extends WebPage {

	private static final long serialVersionUID = 1L;

	// TODO Add any page properties or variables here
	/**
	 * Constructor that is invoked when page is invoked without a session.
	 *
	 * @param parameters
	 *            Page parameters
	 */
	public HomePage(final PageParameters parameters) {

		add(CSSPackageResource.getHeaderContribution("css/style.css"));
		add(new Label("titleMsg", "YouGO"));

		buildLinks();
		buildTabs();

		FeedbackPanel feedback = new FeedbackPanel("feedback");
		add(feedback);

	}

	private void buildLinks() {

		add(new Label("welcomeMsg", "Bonjour " + YougoSession.get().getUser().getFullName()));

		BookmarkablePageLink changePasswordLink = new BookmarkablePageLink("changePasswordLink", HomePage.class);
		changePasswordLink.add(new Label("changePasswordMsg", "Changer le mot de passe"));
		add(changePasswordLink);

		BookmarkablePageLink logoutLink = new BookmarkablePageLink("logoutLink", LogoutPage.class);
		logoutLink.add(new Label("logoutMsg", "Deconnexion"));
		add(logoutLink);
	}

	private void buildTabs() {
		List tabs = new ArrayList();
		if (YougoSession.get().getRoles().hasRole(Roles.USER)) {
			tabs.add(new AbstractTab(new Model<String>("Mes Demandes")) {

				public Panel getPanel(String panelId) {
					return new MesDemandes(panelId);
				}
			});
			tabs.add(new AbstractTab(new Model<String>("Nouvelle Demande")) {

				public Panel getPanel(String panelId) {
					return new NouvelleDemande(panelId);
				}
			});
			tabs.add(new AbstractTab(new Model<String>("Valider Demandes")) {

				public Panel getPanel(String panelId) {
					return new ValiderDemandes(panelId);
				}
			});
		}
		if (YougoSession.get().getRoles().hasRole(Roles.ADMIN)) {
			tabs.add(new AbstractTab(new Model<String>("Gerer Utilisateurs")) {

			public Panel getPanel(String panelId) {
				return new GererUtilisateurs(panelId);
			}
			});
			tabs.add(new AbstractTab(new Model<String>("Gerer Types Utilisateurs")) {

				public Panel getPanel(String panelId) {
					return new GererTypesUtilisateurs(panelId);
				}
			});
			tabs.add(new AbstractTab(new Model<String>("Gerer Types Demandes")) {

				public Panel getPanel(String panelId) {
					return new GererTypesDemandes(panelId);
				}
			});
		}
		add(new TabbedPanel("tabs", tabs));
	}
}
